<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin_m extends Base_model {
	public $primary_key = 'admin_id';
	
    public $id = 0;
    public $name;
    public $account;
    public $avatar;
    public $role_id;
    public $role_name;
	
	public $before_create = array( 'set_password' );
    public $before_update = array( 'set_password' );

    function __construct() {
        parent::__construct();
        $this->setInfo();
    }


    
    protected function set_password($user)
    {
        if(!isset($user['admin_password'])) return $user;
        $this->load->model('admin_m');
        $salt = (isset($user['admin_salt']) ? $user['admin_salt'] : $this->random_salt());

        $user['admin_password'] = $this->hash_pass($user['admin_password'], $salt);
        $user['admin_salt'] = $salt;
        return $user;
    }
    /**
     * Hash password
     * @return String 
     */
    public function hash_pass($pass, $salt = '') {
        return md5(base64_encode(md5($pass.':'.$salt)).$salt);
    }

    /**
     * Random Salt
     * @return String(10)
     */
    public function random_salt() {
        $d = base64_encode(md5(time() . rand()));
        return substr($d, 0, 10);
    }

    public function random_key($num_char = 20) {
        $act = base64_encode(md5(time() . rand()));
        return substr($act, 0, $num_char);
    }

    function update_password($password, $user_id)
    {
        return $password;
        if(!isset($password['admin_salt']))
        {

        }
        if(is_array($password))
        {
            $password = $password['admin_password'];
            if(isset($password['admin_salt']) && $salt =='')
            {
                $salt = $password['admin_salt'];
            }
        }
         if($salt =='')
         {
            $salt = $this->random_salt();
         }

         $hash = $this->hash_pass($password, $salt);
         $this->update($user_id, array(
                'admin_salt' =>$salt
            ));
         if(is_array($password)) {
            $password['admin_salt'] = $salt;
            $password['admin_password'] = $hash;
            return $password;
        }
    }

    function setLogin($user) {
        if (!$user) {
            return false;
        }
        $data = array(
            'adm_id' => $user->admin_id,
            'adm_account' => $user->admin_account,
            'adm_name' => $user->display_name,
            'adm_email' => $user->admin_email,
            'adm_avatar' => $user->admin_avatar,
            'role_id' => $user->role_id,
            'role_name' => $user->role_name,
            'isAdminLogin' => 1,
        );
        $this->session->set_userdata($data);
        return true;
    }

    public function checkLogin() {
        return $this->session->userdata('isAdminLogin');
    }

    function logout() {
        $this->session->sess_destroy();
        return 1;
    }

    function setInfo() {
        if ($this->checkLogin() && $this->id ==0) {
            $this->id = $this->session->userdata('adm_id');
            $this->account = $this->session->userdata('adm_account');
            $this->name = ($this->session->userdata('adm_name') =='')?$this->account:$this->session->userdata('adm_name');
            $this->avatar = $this->session->userdata('adm_avatar');
            $this->role_id = $this->session->userdata('role_id');
            $this->role_name = $this->session->userdata('role_name');
        }
		
    }

	function compare_password($password = '', $user_password = '', $salt = '')
	{
		if($password !='' && $user_password != '' && $salt !='')
		{
			$hash_pass = $this->hash_pass($password,$salt);
				if($hash_pass == $user_password)
					return true;
		}
		return false;
	}
	
}

?>